package action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import common.BaseAction;
import dicts.RealmDict;
import entities.TLogic;
import interfaces.ILogicService;
import net.sf.json.JSONObject;
import util.HttpUtil;
import util.LogUtil;
import utils.StringUtil;

public class LogicAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private ILogicService logicService;

	private TLogic logicBean;
	private List<TLogic> logicList;

	private String logNo;
	private String logNos;
	private String txnNo;
	private String logName;
	private String logOrder;
	private String logState;
	private Date logDate;
	private Date logDateTop;
	private Date logDateEnd;
	private String logRemark;

	// 进行单笔物流上传
	public void save() throws Exception {
		JSONObject resultMap = new JSONObject();
		try {
			Map<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("txnNo", txnNo);
			paramsMap.put("logName", logName);
			paramsMap.put("logOrder", logOrder.toUpperCase());
			resultMap = JSONObject.fromObject(new HttpUtil().post(RealmDict.GATEWAY + "Logic", paramsMap));
			ServletActionContext.getResponse().getWriter().print(resultMap.toString());
			return;
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("code", "9999");
			resultMap.put("message", "connection exception");
			ServletActionContext.getResponse().getWriter().print(resultMap.toString());
			return;
		}
	}

	public String editbatch() throws Exception {
		return SUCCESS;
	}

	// 进行物流上传
	public String savebatch() throws Exception {
		JSONObject resultMap = new JSONObject();
		Map<String, String> paramsMap = new HashMap<String, String>();

		if (StringUtil.isEmpty(text)) {
			addActionError("上传列表不能为空");
			return INFO;
		}
		String[] contents = text.split("\r\n");
		if (contents.length == 0) {
			addActionError("上传的数据不符合格式");
			return INFO;
		}
		int step = 0;
		for (int i = 0; i < contents.length; i++) {
			String line = contents[i];
			try {
				String[] data = line.split("\\|");
				paramsMap.put("txnNo", data[0].trim());
				paramsMap.put("logName", data[1].trim());
				paramsMap.put("logOrder", data[2].trim());
				paramsMap.put("logType", "1");
			} catch (Exception e) {
				addActionError("第" + (i + 1) + "行数据格式错误");
				continue;
			}

			try {
				resultMap = JSONObject.fromObject(new HttpUtil().post(RealmDict.GATEWAY + "Logic", paramsMap));
			} catch (Exception e) {
				LogUtil.exception("savelogic_json");
				addActionError("第" + (i + 1) + "行网络不通无法获取结果");
				continue;
			}

			if (!resultMap.getString("code").equals("0000")) {
				addActionError("第" + (i + 1) + "行失败,原因[" + resultMap.getString("message") + "]");
				continue;
			}
			addActionError("第" + (i + 1) + "行成功上传");
			step = step + 1;
		}
		addActionError("共成功 " + step + " 条数据");
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public ILogicService getLogicService() {
		return logicService;
	}

	public void setLogicService(ILogicService logicService) {
		this.logicService = logicService;
	}

	public TLogic getLogicBean() {
		return logicBean;
	}

	public void setLogicBean(TLogic logicBean) {
		this.logicBean = logicBean;
	}

	public List<TLogic> getLogicList() {
		return logicList;
	}

	public void setLogicList(List<TLogic> logicList) {
		this.logicList = logicList;
	}

	public String getLogNo() {
		return logNo;
	}

	public void setLogNo(String logNo) {
		this.logNo = logNo;
	}

	public String getLogNos() {
		return logNos;
	}

	public void setLogNos(String logNos) {
		this.logNos = logNos;
	}

	public String getTxnNo() {
		return txnNo;
	}

	public void setTxnNo(String txnNo) {
		this.txnNo = txnNo;
	}

	public String getLogName() {
		return logName;
	}

	public void setLogName(String logName) {
		this.logName = logName;
	}

	public String getLogOrder() {
		return logOrder;
	}

	public void setLogOrder(String logOrder) {
		this.logOrder = logOrder;
	}

	public String getLogState() {
		return logState;
	}

	public void setLogState(String logState) {
		this.logState = logState;
	}

	public Date getLogDate() {
		return logDate;
	}

	public void setLogDate(Date logDate) {
		this.logDate = logDate;
	}

	public Date getLogDateTop() {
		return logDateTop;
	}

	public void setLogDateTop(Date logDateTop) {
		this.logDateTop = logDateTop;
	}

	public Date getLogDateEnd() {
		return logDateEnd;
	}

	public void setLogDateEnd(Date logDateEnd) {
		this.logDateEnd = logDateEnd;
	}

	public String getLogRemark() {
		return logRemark;
	}

	public void setLogRemark(String logRemark) {
		this.logRemark = logRemark;
	}

}
